package com.example.dangerouscargodriver.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.entry.MarkInfo;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

public class EvaluateOrderDetailInfoActivity extends HttpRequestActivity {

    private static final String TAG = "EvaluateOrderDetailInfo";
    private ActionBar mActionBar = null;

    private String mRecId = null;

    private MarkInfo mMarkInfo = null;

    private LinearLayout mLayoutMarkGradeOne = null;
    private LinearLayout mLayoutMarkGradeTwo = null;
    private LinearLayout mLayoutMarkGradeThree = null;
    private LinearLayout mLayoutMarkGradeFour = null;
    private LinearLayout mLayoutMarkGradeFive = null;

    private LinearLayout mLayoutMarkContent = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initHttpComponent(EvaluateOrderDetailInfoActivity.class);
        setContentView(R.layout.activity_evaluate_order_detail_info);

        mRecId = getIntent().getStringExtra("id");

        mLayoutMarkGradeOne = findViewById(R.id.layout_star_one);
        mLayoutMarkGradeTwo = findViewById(R.id.layout_star_two);
        mLayoutMarkGradeThree = findViewById(R.id.layout_star_three);
        mLayoutMarkGradeFour = findViewById(R.id.layout_star_four);
        mLayoutMarkGradeFive = findViewById(R.id.layout_star_five);

        mLayoutMarkContent = findViewById(R.id.layout_mark_content);

        mActionBar = findViewById(R.id.actionBarEvaluateOrderDetailInfo);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        executeGetOrderEvaluateInfoAction();
    }

    private void executeGetOrderEvaluateInfoAction(){
        HashMap<String, String> header = new HashMap<>();
        header.put("token", AuthController.Instance().getToken());

        HashMap<String, String> param = new HashMap<>();
        param.put("id", mRecId);

        sendHttpRequest(IHttpRequest.RequestMethod.GET_METHOD,
                RemoteAPI.REQUEST_V1_USER_SG_GET_MARK,
                header,
                param,
                RemoteAPICmd.REQUEST_V1_USER_SG_GET_MARK);
    }

    private void refreshMarkViewInfo(){
        if(mMarkInfo != null){
            int scoreInt = Integer.parseInt(mMarkInfo.getScore());
            if(scoreInt == 1){
                mLayoutMarkGradeOne.setVisibility(View.VISIBLE);
            }
            else if(scoreInt == 2){
                mLayoutMarkGradeTwo.setVisibility(View.VISIBLE);
            }
            else if(scoreInt == 3){
                mLayoutMarkGradeThree.setVisibility(View.VISIBLE);
            }
            else if(scoreInt == 4){
                mLayoutMarkGradeFour.setVisibility(View.VISIBLE);
            }
            else if(scoreInt == 5){
                mLayoutMarkGradeFive.setVisibility(View.VISIBLE);
            }
            else{

            }

            List<String> markContent = mMarkInfo.getContent();
            mLayoutMarkContent.removeAllViews();
            if(markContent != null){
                for(int index = 0; index < markContent.size(); index++){
                    View layout = LayoutInflater.from(this).inflate(R.layout.layout_mark_info_item, null);
                    TextView textView = layout.findViewById(R.id.tv_mark_info_item);
                    textView.setText(markContent.get(index));
                    mLayoutMarkContent.addView(layout);
                }
            }
        }
    }

    @Override
    protected void onHttpResponse(int actionId, String response) {
        try {
            JSONObject res = new JSONObject(response);
            Gson gson = new Gson();
            Log.d(TAG, "onResponse: " + response);
            if(actionId == RemoteAPICmd.REQUEST_V1_USER_SG_GET_MARK){
                if(res.getString("status").equals("1")){
                    mMarkInfo = gson.fromJson(res.getJSONObject("message").getString("mark"), MarkInfo.class);

                    if(mMarkInfo != null){
                        refreshMarkViewInfo();
                    }
                }
                else{
                    ToastUtils.showLongToast(this, "获取评价信息失败");
                }
            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onHttpErrorResponse(int actionId, String response) {
        if(actionId == RemoteAPICmd.REQUEST_V1_USER_SG_GET_MARK){

        }
    }
}